package com.github.shiqiyue.app.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/****
 * 自定义公共字段填充
 *
 * @author wwy
 *
 * @since 2020年4月26日
 *
 */
public class CustomMetaObjectHandler implements MetaObjectHandler {

    private static final String ADD_DATE = "addDate";

    private static final String EDIT_DATE = "editDate";


    @Override
    public void insertFill(MetaObject metaObject) {
        Date now = new Date();
        Object addDate = getFieldValByName(ADD_DATE, metaObject);
        if (addDate == null) {
            setFieldValByName(ADD_DATE, now, metaObject);
        }
        Object editDate = getFieldValByName(EDIT_DATE, metaObject);
        if (editDate == null) {
            setFieldValByName(EDIT_DATE, now, metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName(EDIT_DATE, new Date(), metaObject);

    }

}